package lambda;

import java.util.Comparator;

import java.util.Arrays;
import java.util.List;

/**
 * refs: https://www.cnblogs.com/franson-2016/p/5593080.html
 * <p>
 * Lambda表达式的本质是一个"语法糖",由编译器推断并帮你转换包装为常规的代码,因此你可以使用更少的代码来实现同样的功能。
 * <p>
 * Lambda表达式的语法 基本语法: (parameters) -> expression 或 (parameters) ->{ statements; }
 */
public class LamadaTest {


  public static void main(String[] args) {

    forEachTest();

    runnableTest();
  }


  static void forEachTest() {
    String[] atp = {"Rafael Nadal", "Novak Djokovic",
        "Stanislas Wawrinka",
        "David Ferrer", "Roger Federer",
        "Andy Murray", "Tomas Berdych",
        "Juan Martin Del Potro"};

    List<String> players = Arrays.asList(atp);

    // 以前的循环方式
    for (String player : players) {
      System.out.print(player + "; ");
    }

    System.out.println("=====lambda=======1");
    // 使用 lambda 表达式以及函数操作(functional operation)
    players.forEach((player) -> System.out.print(player + "; "));

    System.out.println("=====lambda=======2");
    // 在 Java 8 中使用双冒号操作符(double colon operator)
    players.forEach(System.out::println);

    System.out.println("=====lambda=======2");
    // 在 Java 8 中使用双冒号操作符(double colon operator)

  }


  static void runnableTest() {
    // 1.1使用匿名内部类
    new Thread(new Runnable() {
      @Override
      public void run() {
        System.out.println("Hello world !");
      }
    }).start();

    // 1.2使用 lambda expression
    new Thread(() -> System.out.println("Hello world !")).start();

    // 2.1使用匿名内部类
    Runnable race1 = new Runnable() {
      @Override
      public void run() {
        System.out.println("Hello world !");
      }
    };

    // 2.2使用 lambda expression
    Runnable race2 = () -> System.out.println("Hello world !");

    // 直接调用 run 方法(没开新线程哦!)
    race1.run();
    race2.run();
  }

  static void sortTest() {
    String[] players = {"Rafael Nadal", "Novak Djokovic",
        "Stanislas Wawrinka", "David Ferrer",
        "Roger Federer", "Andy Murray",
        "Tomas Berdych", "Juan Martin Del Potro",
        "Richard Gasquet", "John Isner"};

// 1.1 使用匿名内部类根据 name 排序 players
    Arrays.sort(players, new Comparator<String>() {
      @Override
      public int compare(String s1, String s2) {
        return (s1.compareTo(s2));
      }
    });

    // 1.1 使用匿名内部类根据 surname 排序 players
    Arrays.sort(players, new Comparator<String>() {
      @Override
      public int compare(String s1, String s2) {
        return (s1.substring(s1.indexOf(" ")).compareTo(s2.substring(s2.indexOf(" "))));
      }
    });

// 1.2 使用 lambda expression 排序,根据 surname
    Comparator<String> sortBySurname = (String s1, String s2) ->
        (s1.substring(s1.indexOf(" ")).compareTo(s2.substring(s2.indexOf(" "))));
    Arrays.sort(players, sortBySurname);

// 1.3 或者这样,怀疑原作者是不是想错了,括号好多...
    Arrays.sort(players, (String s1, String s2) ->
        (s1.substring(s1.indexOf(" ")).compareTo(s2.substring(s2.indexOf(" "))))
    );

// 2.1 使用匿名内部类根据 name lenght 排序 players
    Arrays.sort(players, new Comparator<String>() {
      @Override
      public int compare(String s1, String s2) {
        return (s1.length() - s2.length());
      }
    });

// 2.2 使用 lambda expression 排序,根据 name lenght
    Comparator<String> sortByNameLenght = (String s1, String s2) -> (s1.length() - s2.length());
    Arrays.sort(players, sortByNameLenght);

// 2.3 or this
    Arrays.sort(players, (String s1, String s2) -> (s1.length() - s2.length()));

    // 3.1 使用匿名内部类排序 players, 根据最后一个字母
    Arrays.sort(players, new Comparator<String>() {
      @Override
      public int compare(String s1, String s2) {
        return (s1.charAt(s1.length() - 1) - s2.charAt(s2.length() - 1));
      }
    });

// 3.2 使用 lambda expression 排序,根据最后一个字母
    Comparator<String> sortByLastLetter =
        (String s1, String s2) ->
            (s1.charAt(s1.length() - 1) - s2.charAt(s2.length() - 1));
    Arrays.sort(players, sortByLastLetter);

// 3.3 or this
    Arrays.sort(players,
        (String s1, String s2) -> (s1.charAt(s1.length() - 1) - s2.charAt(s2.length() - 1)));
  }
}
